package com.misakyanls.examples;

import org.apache.commons.lang3.StringUtils;

public class ConvertIp {

	private static String intToStrByMove(long ip) {
		StringBuilder result = new StringBuilder();

		for (int i = Byte.SIZE * 3; i >= 0; i -= Byte.SIZE) {
			result.append(Long.toString((ip >> i) & 0x000000FF)
					+ (i != 0 ? "." : ""));
		}
		return result.toString();
	}

	private static String intToStrByRepresent(long ip) {
		StringBuilder result = new StringBuilder();
		String hex = StringUtils.leftPad(Long.toHexString(ip), 8, '0');

		for (int i = 0; i < 8; i += 2) {
			int curByte = Integer.parseInt(hex.substring(i, i + 2), 16);
			result.append((i != 0 ? "." : "") + Integer.toString(curByte));
		}
		return result.toString();
	}

	public static void main(String[] args) {
		long startTime = System.currentTimeMillis();
		System.out.println(intToStrByRepresent(0L));
		System.out.println(intToStrByRepresent(Long.parseLong("ff89ffff", 16)));
		long stopTime = System.currentTimeMillis();
		long elapsedTime = stopTime - startTime;
		System.out.println("Repr - " + elapsedTime);

		startTime = System.currentTimeMillis();
		System.out.println(intToStrByMove(0L));
		System.out.println(intToStrByMove(Long.parseLong("ff89ffff", 16)));
		stopTime = System.currentTimeMillis();
		elapsedTime = stopTime - startTime;
		System.out.println("Move - " + elapsedTime);
	}

}
